<?php

function list_jabatan_existing(){
	
	$header = array(
		'No. Organisasi '=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'No. Posisi'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	$com = isset($_GET['com']) ? $_GET['com']: '';
	$task = isset($_GET['task']) ? $_GET['task']: '';
	$field = isset($_GET['field']) ? $_GET['field']: '';
	$key = isset($_GET['key']) ? $_GET['key']: '';
	if(isset($_GET['key']) ){
		$query = "SELECT a.* FROM jabatan a	
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id
			WHERE b.active='Y' 
			AND (a.nama_jabatan LIKE '%{$_GET['key']}%' 
			OR a.nomor_posisi ='{$_GET['key']}')
			ORDER BY a.urut " ;
	}else{
		$query = "SELECT a.* FROM jabatan a	
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id
			WHERE b.active='Y' 
			ORDER BY a.urut" ;
	}
	
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	
	$pager_url  ="index.php?com={$com}&task={$task}&field={$field}&key={$key}&page=";	
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array();  
	while($ey = my_fetch_array($result) ){
		$query = "SELECT * FROM kekaryawanan 
			WHERE jabatan_id =".$ey[jabatan_id]."  
			AND karyawan_id NOT IN (select karyawan_id FROM pemberhentian)
			ORDER BY tmb ASC";
		$karyawans = my_query($query);
		$nomor_organisasi=substr($ey['nomor_posisi'],0,4) ;
		$nomor_posisi =substr($ey['nomor_posisi'],4,4) ;

		$i = 0;
		while($row1 = my_fetch_array($karyawans) ){
			$i++; 
			$karyawan = loaddata_karyawan($row1['karyawan_id']);
			$row[] = array(
				'nomor_org'=> ($i > 1 ) ?  ' ' : $nomor_organisasi,
				'jabatan'=>  ($i > 1 ) ?  ' ' : $ey['nama_jabatan'] ,
				'nomor_pos'=>  $nomor_posisi.'.'.sprintf('%02d' ,$i)	 ,
				'NIK'=>$karyawan['nik'],
				'karyawan'=>$karyawan['nama_gelar'],
				'operasi'=> position_text_align(  ' ' , 'right'), 
			);			
		}	
		if($i==0){
			$row[] = array(
				'nomor_org'=> $nomor_organisasi,
				'jabatan'=>  $ey['nama_jabatan'] ,
				'nomor_pos'=>  $nomor_posisi.'.00',
				'nik'=> ' ',
				'karyawan'=> ' ',
				'operasi'=> position_text_align(  ' ' , 'right'), 
			);
		}	
	}
	$datas = table_rows($row);
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	$menuontop = array(
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=jabatan_existing\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
	);
	$printmenu = additional_menu_on_list( $menuontop ,$search ); 
	$paging = $kgPagerOBJ ->showPaging();	
	return $printmenu.table_builder($header , $datas ,  6 , false ,$paging);
}

function pemegang_jabatan($jabatan_id){
	$query = "SELECT * FROM kekaryawanan WHERE jabatan_id = {$jabatan_id} ";
	$res = my_query($query);
	
	$view = '';
	while($row = my_fetch_array($res) ){
		$karyawan = loaddata_karyawan($row['karyawan_id']);
		$view .= ucfirst($karyawan['nama']) . ', ';
		
	}
	return trim( trim($view) , ",");
}














